package ma.platform.server.student.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import ma.plaform.server.common.log.annotation.SysLog;
import ma.platform.server.core.exception.CheckedException;
import ma.platform.server.core.util.BaseConvert;
import ma.platform.server.core.util.R;
import ma.platform.server.student.entity.*;
import ma.platform.server.student.entity.TbComboInfo;
import ma.platform.server.student.req.QueryDateComboReq;
import ma.platform.server.student.service.TbDateComboService;
import ma.platform.server.student.service.TbSchoolService;
import org.apache.commons.collections.CollectionUtils;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/dateCombo")
public class ComboInfoContrller {

    @Resource
    private TbDateComboService tbDateComboService;

    @Resource
    private TbSchoolService schoolService;


    @GetMapping("/listDate")
    public R listDate(@ParameterObject QueryDateComboReq req) {
        return R.ok(tbDateComboService.listDate(req));
    }


    /**
     * 添加学生
     *
     * @return success、false
     */
    @SysLog("添加套餐详情")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('tb_dateCombo_add')")
    public R save(@Valid @RequestBody TbDateCombo dateCombo) {
        List<TbDateCombo> dateCombos = Lists.newArrayList();

        if (CollectionUtils.isNotEmpty(dateCombo.getSchoolIdList())) {
            for (Long schoolId : dateCombo.getSchoolIdList()) {
                TbDateCombo combo = BaseConvert.convert(dateCombo, TbDateCombo.class);
                combo.setSchoolId(schoolId);
                dateCombos.add(combo);
            }
        }else {
            dateCombos.add(dateCombo);
        }
//        List<TbSchool> schoolList = schoolService.list();
//        for (TbSchool school : schoolList) {
//            TbDateCombo combo = BaseConvert.convert(dateCombo, TbDateCombo.class);
//            combo.setSchoolId(school.getId());
//            dateCombos.add(combo);
//        }
        tbDateComboService.saveBatch(dateCombos);
        return R.ok(dateCombo);
    }

    /**
     * 删除学生
     *
     * @param ids ID
     * @return R
     */
    @SysLog("删除套餐详情")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('tb_dateCombo_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(tbDateComboService.removeByIds(Lists.newArrayList(ids)));
    }

    /**
     * 修改学生
     *
     * @return success/false
     */
    @PutMapping
    @SysLog("修改套餐详情")
    @PreAuthorize("@pms.hasPermission('tb_dateCombo_edit')")
    public R updateById(@Valid @RequestBody TbDateCombo dateCombo) {
        tbDateComboService.updateById(dateCombo);
        return R.ok();
    }

    /**
     * 通过ID查询信息
     *
     * @param id ID
     */
    @GetMapping("/details/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(tbDateComboService.getById(id));
    }

    @GetMapping("/copyCombo/{comboId}")
    public R copyCombo(@PathVariable Long comboId, @RequestParam("currentDate") String currentDate) {
        tbDateComboService.copyCombo(comboId, currentDate);
        return R.ok();
    }
}
